/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ipti.br.tag.bean.validator;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.FacesValidator;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

/**
 *
 * @author ipti
 */
@FacesValidator(value="yearV")
public class YearValidator implements Validator{

    @Override
    public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
        int i = (Integer) value;
        int min = getAtributo("minimo", component);
        int max = getAtributo("maximo", component);
        if(i < min || i > max ){
            FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR,
                    "Erro de validação", "o ano deve estar entre "+min+" e "+max);
            throw new ValidatorException(msg);
        }
    }
    
    private Integer getAtributo(String s,UIComponent component){
        return Integer.parseInt((String) component.getAttributes().get(s));
    }
}
